package com.sctvcloud.renhe.ui.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.ui.widget.PickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 时间选择的Dialog
 */
public class TimePopupDialog extends BaseDialogFragment implements View.OnClickListener {

    private CustomFontTextView sure_cv;
    private PopItemClick popItemClick;
    private PickerView year_pv, month_pv, day_pv;
    private View diaView;

    public TimePopupDialog(){}

    @Override
    protected Dialog createDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);// 外部点击取消
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        return dialog;
    }

    @Override
    protected int getContentRes() {
        return R.layout.dia_time_choice;
    }

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        year_pv = (PickerView) dialog.findViewById(R.id.year_pv);
        month_pv = (PickerView) dialog.findViewById(R.id.month_pv);
        day_pv = (PickerView) dialog.findViewById(R.id.date_pv);
        sure_cv = (CustomFontTextView) dialog.findViewById(R.id.sure_cv);
        sure_cv.setOnClickListener(this);
        buildPicker(year_pv, month_pv, day_pv);
    }

    private void buildPicker(PickerView year_pv, PickerView month_pv, PickerView day_pv) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH);
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH) - 1;
        List<String> yearDatas = new ArrayList<>();
        for (int i = currentYear - 60; i <= currentYear; i++) {
            yearDatas.add(i + "");
        }
        year_pv.setData(yearDatas, 60);

        List<String> monthDatas = new ArrayList<>();
        for (int j = 1; j <= 12; j++) {
            monthDatas.add(j + "");
        }
        month_pv.setData(monthDatas, currentMonth);
        List<String> dayDates = new ArrayList<>();
        for (int k = 1; k <= 31; k++) {
            dayDates.add(k + "");
        }
        day_pv.setData(dayDates, currentDay);
    }

    @Override
    public void onClick(View v) {
        if (popItemClick != null) {
            popItemClick.selectTime(year_pv.getSelected(),month_pv.getSelected(),day_pv.getSelected());
        }
        dismissAllowingStateLoss();
    }

    public TimePopupDialog setPopItemClick(PopItemClick popItemClick) {
        this.popItemClick = popItemClick;
        return this;
    }

    public interface PopItemClick {
        void selectTime(String year,String month,String day);
    }
}
